inspector: Parse custom css with a delay
authorMatthias Clasen <mclasen@redhat.com>
Tue, 3 Jun 2014 13:44:28 +0000 (09:44 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Tue, 3 Jun 2014 13:44:28 +0000 (09:44 -0400)
The CSS editor was feeling a little sluggish, because it was
reparsing and reapplying the CSS on every keystroke. Add a small
delay, to make this feel smoother.

gtk/inspector/css-editor.c

index 10c63b970ce62e7e071872fa31a7e8a6f32b5024..8224c6ae38a44ad989fee9a54fd55e486d312ea5 100644 (file)
@@ -55,6 +55,7 @@ struct _GtkInspectorCssEditorPrivate
   gboolean global;
   GtkStyleContext *context;
   GtkToggleToolButton *disable_button;
+  guint timeout;
 };
 
 G_DEFINE_TYPE_WITH_PRIVATE (GtkInspectorCssEditor, gtk_inspector_css_editor, GTK_TYPE_BOX)
@@ -207,8 +208,7 @@ apply_system_font (GtkInspectorCssEditor *ce)
 }
 
 static void
-text_changed (GtkTextBuffer         *buffer,
-              GtkInspectorCssEditor *ce)
+update_style (GtkInspectorCssEditor *ce)
 {
   GtkCssProvider *provider;
   gchar *text;
@@ -220,13 +220,35 @@ text_changed (GtkTextBuffer         *buffer,
   else
     return;
 
-  text = get_current_text (buffer);
+  text = get_current_text (ce->priv->text);
   gtk_css_provider_load_from_data (provider, text, -1, NULL);
   g_free (text);
 
   gtk_style_context_reset_widgets (gdk_screen_get_default ());
 }
 
+static gboolean
+update_timeout (gpointer data)
+{
+  GtkInspectorCssEditor *ce = data;
+
+  ce->priv->timeout = 0;
+
+  update_style (ce);
+
+  return G_SOURCE_REMOVE;
+}
+
+static void
+text_changed (GtkTextBuffer         *buffer,
+              GtkInspectorCssEditor *ce)
+{
+  if (ce->priv->timeout != 0)
+    g_source_remove (ce->priv->timeout);
+
+  ce->priv->timeout = g_timeout_add (100, update_timeout, ce); 
+}
+
 static void
 show_parsing_error (GtkCssProvider        *provider,
                     GtkCssSection         *section,
@@ -337,6 +359,17 @@ set_property (GObject      *object,
     }
 }
 
+static void
+finalize (GObject *object)
+{
+  GtkInspectorCssEditor *ce = GTK_INSPECTOR_CSS_EDITOR (object);
+
+  if (ce->priv->timeout != 0)
+    g_source_remove (ce->priv->timeout);
+
+  G_OBJECT_CLASS (gtk_inspector_css_editor_parent_class)->finalize (object);
+}
+
 static void
 gtk_inspector_css_editor_class_init (GtkInspectorCssEditorClass *klass)
 {
@@ -346,6 +379,7 @@ gtk_inspector_css_editor_class_init (GtkInspectorCssEditorClass *klass)
   object_class->get_property = get_property;
   object_class->set_property = set_property;
   object_class->constructed = constructed;
+  object_class->finalize = finalize;
 
   g_object_class_install_property (object_class, PROP_GLOBAL,
       g_param_spec_boolean ("global", "Global", "Whether this editor changes the whole application or just the selected widget",